<?php

/**
 * Description of LoginEgresados
 *
 * @author JeyDotC
 * @export Modules
 * @export-metadata default-module
 */
class Login extends Module {

    private $userName = "";
    private $password = "";
    private $returnURL = "";

    function index() {
        
        /*$this->returnURL = $this->request->get("return", $this->getDefaultReturnURL());
        $this->assign("recoverUsernameLink", $this->route->action("recoverUserId"));
        $this->assign("action", "http://egresados.usbcartagena.edu.co/?option=com_acaweb&Itemid=28&controller=Egresados.Login&task=doLogin&return={$this->returnURL}");
        /*$this->assign("action", $this->route->action("doLogin"));
        $this->assign("lastUsername", $this->userName);*/
        
        $this->assign("action", $this->route->moduleAction("ActualizarDatos")."&NoFrame=1");
        
        $this->display("index");
    }
	
	function doLogin() {
		if ($this->request->submitted()) {
            $this->userName = $this->request->post("Username");
            $this->password = $this->request->post("Password");
            $this->returnURL = base64_decode($this->request->get("return", $this->getDefaultReturnURL()));
			
            $result = $this->authenticate();
                        
            if ($result) {
                $this->route->redirect($this->returnURL . "&NoFrame=1");
            } else {
                $this->addMessage(new Message(
                                "La informaci&oacute;n de nombre de usuario o contrase&ntilde;a introducida no es correcta",
                                "",
                                Message::ERROR
                ));
            }
        }
        $this->index();
    }

    function recoverUserId() {
        $this->assign("action", $this->route->action("showUserId"));
        $this->display("recoverUserId");
    }

    function showUserId() {
        if ($this->request->submitted()) {
            $documento = $this->request->post("NumeroIdentificacion");
            $studentsList = $this->model->students->GetByDocumentoIdentidad($documento);

            $isregistered = count($studentsList) > 0;
            if (!$isregistered) {
                $this->addMessage(new Message(
                                "No tenemos registrado ningun estudiante con ese documento de identidad, tal vez aparezcas con otro documento.",
                                "No apareces registrado",
                                Message::ERROR));
            }

            $this->assign("isRegistered", $isregistered);
            $this->assign("studenstList", $studentsList);
            $this->assign("retryLink", $this->route->action("recoverUserId"));
            $this->assign("returnLink", $this->route->action("index"));
            $this->display();
        } else {
            $this->recoverUserId();
        }
    }

    private function authenticate() {
        $mainframe = JFactory::getApplication();        
        $studentList = $this->model->students->GetByDocumentoIdentidad($this->userName);

        $documento = "";

        if (!empty ($studentList)){
                $documento = $studentList[0]->getCodigoEstudiante();
        }
		
        $credentials = array("username" => $documento, "password" => $this->password,);        
        
        return $mainframe->login($credentials, array("silent" => true));
    }

    public function getDefaultReturnURL() {
        return base64_encode("http://egresados.usbcartagena.edu.co/?option=com_acaweb&Itemid=28&controller=Egresados.ActualizarDatos&task=index&NoFrame=1");
    }

}

?>
